<?php
/**
 * 
 * 
 */
class TSendMail {
	
	

	private static $_debugUser = 'peng.shan';
	
	private static $_message = array(
		1 => '有新的调动申请，等待您的审批。{auditUrl}',
		2 => '调动申请被 *** 驳回，请检查！{auditUrl}',
		3 => '调动申请被审核完成！{detailUrl}',
		4 => '*** 调动申请审核完成，给璇姐通知',
		5 => '*** 调动申请审核完成，给IT通知',
		6 => '您发起的调动申请，已经被***取消.',
	);

	private static $_template = '<b>***</b><br />系统登陆地址: <a href="http://local.hr/">http://local.hr/</a><br />';

	public static function _getBody($msgId,$userName='',$transferId=0,$transferHtml = '')
	{
		$auditUrl = '<a href="'.Yii::app()->params['siteUrl'].'transferAudit/index?id='.$transferId.'">'.Yii::app()->params['siteUrl'].'transferAudit/index?id='.$transferId.'</a>';
		$detailUrl = '<a href="'.Yii::app()->params['siteUrl'].'transfer/detail?id='.$transferId.'">'.Yii::app()->params['siteUrl'].'transfer/detail?id='.$transferId.'</a>';
		$msg = str_replace('***',$userName,self::$_message[$msgId]);
		$msg = str_replace('{auditUrl}',$auditUrl,$msg);
		$msg = str_replace('{detailUrl}',$detailUrl,$msg);
		$msg = str_replace('***',$msg,self::$_template);

		if($transferHtml != '')
		{
			$msg .= $transferHtml;
		}

		return $msg;
	}
	
	public static function _sendMail($m) 
	{
		include_once 'phpmailer/class.phpmailer.php';
		$oPHPMailer = new PHPMailer();
		$oPHPMailer->CharSet = Yii::app()->params['smtp']['CharSet'];
		$oPHPMailer->Encoding = Yii::app()->params['smtp']['Encoding'];
		$oPHPMailer->Mailer = 'smtp';
		$oPHPMailer->SMTPAuth = true;
		$oPHPMailer->SMTPSecure = 'tls';
		$oPHPMailer->Host = Yii::app()->params['smtp']['Host'];
		$oPHPMailer->Port = Yii::app()->params['smtp']['Port'];
		$oPHPMailer->Username = Yii::app()->params['smtp']['Username'];
		$oPHPMailer->Password = Yii::app()->params['smtp']['Password'];
		$oPHPMailer->From = Yii::app()->params['smtp']['Username'].'@happyelements.com';
		$oPHPMailer->FromName = 'Happy Elements HR系统';
		$oPHPMailer->Subject = 'Happy Elements HR系统提醒！';
		$oPHPMailer->Body = $m['body'];
		$oPHPMailer->IsHTML(true);
		$oPHPMailer->AddAddress($m['to'].'@happyelements.com');
		if($oPHPMailer->send())
		{
			return true;
		} 
		else 
		{
			return false;
		}
	}
	
	//
	public static function send($nowUserInfo,$dataAll,$transferHtml) 
	{
		$data = isset($dataAll['transfer']) ? $dataAll['transfer'] : $dataAll;
		if($data->status == 1) 
		{ 
			//流程中 给下一步发
			$mail['body'] = self::_getBody(1,'',$data->id);
			$mail['to'] = $data->next_username;
		}
		else if($data->status == 2) 
		{
			//审核成功 给发起人
			$mail['body'] = self::_getBody(3,$data->start_username,$data->id,$transferHtml);
			$mail['to'] = $data->start_username;
			//发信给相关人员 
			//option 中的
			$hrOptionModel = new HrOption;
			$res = $hrOptionModel->findAllByAttributes(array(),' option_name like "transfer_mailto%" ');
			if($res)
			{
				foreach ($res as $key => $value)
				{
					$m['body'] = self::_getBody(3,$data->start_username,$data->id,$transferHtml);
					$m['to'] = $value->option_value;
					self::_sendMail($m);
				}
			}
			//被调动人的
			$tu = explode(',', $data->transfer_usernames);
			if($tu)
			{
				foreach ($tu as $key => $value) 
				{
					$_mail['body'] = self::_getBody(3,$data->start_username,$data->id,'');
					$_mail['to'] = $value;
					self::_sendMail($_mail);
				}
			}
			//原部门直接主管 新部门直接主管
			foreach ($data->process as $value)
			{
				if($value->flag == TransferProcess::flagNewDiectManager || $value->flag == TransferProcess::flagOldDiectManager)
				{
					$_mail['body'] = self::_getBody(3,$data->start_username,$data->id,'');
					$m = HecUser::model()->findByAttributes(array('unique_id' => $value->unique_id));
					$_mail['to'] = $m->username;
					self::_sendMail($_mail);
				}
			}
		}
		else if($data->status == -1) 
		{
			//被驳回 给上一步发信
			$mail['body'] = self::_getBody(2,'',$data->id);
			$mail['to'] = $data->next_username;
		}
		else if($data->status == -2) 
		{
			//取消 给发起者发信
			$mail['body'] = self::_getBody(6,$nowUserInfo['c_username'],$data->id);
			$mail['to'] = $data->start_username;
		}
		if(Yii::app()->params['sysDebug'])
		{
			$mail['to'] = self::$_debugUser;
		}
		return self::_sendMail($mail);
	}

	
}